package cn.com.generaldata.os.service.indicator_evaluation.prosperity_analysis;

import org.apache.commons.lang3.Validate;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.jsme_indicator_system.util.Constants;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 进出口总额增长率评估指标计算类.
 */
public class TotalExportImportVolumeGrowthRateCalculateService extends IndicatorEvaluationCalculateService {

	private String totalExportImportVolumeIndicatorName = "江苏省按经营单位所在地进出口总额";

	private String rmbExchangeFateIndicatorName = "人民币汇率（100美元）";

	private String CPIIndicatorName = "CPI";

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double result = null;
		if (indicator.getSection() == Constants.INDICATOR_LEVEL_PROVINCE) {
			result = calculateProvinceResult(indicator, indicatorAccountVO);
		} else if (indicator.getSection() == Constants.INDICATOR_LEVEL_CITY) {
			// 市指标没确定先保留
		}
		return result;
	}

	/**
	 * 计算江苏省进出口总额增长率评估指标计算类.
	 * 
	 * @param indicator
	 * @param indicatorAccountVO
	 * @return
	 */
	private Double calculateProvinceResult(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double comparePrice = getRmbTotalExportImportVolumeCp(indicatorAccountVO, indicatorAccountVO.getYear());
		Double lastRmbTotalExportImportVolume = getRmbTotalExportImportVolume(indicatorAccountVO,
				indicatorAccountVO.getYear() - 1);
		return comparePrice / lastRmbTotalExportImportVolume - 1;
	}

	/**
	 * 获得江苏省进出口总额现价人民币单位
	 * 
	 * @param indicatorAccountVO
	 * @return
	 */
	private Double getRmbTotalExportImportVolume(IndicatorAccountVO indicatorAccountVO, int year) {
		Double totalExportImportVolumeDollar = getInputOrAccountingIndicatorValue(indicatorAccountVO,
				year, totalExportImportVolumeIndicatorName);
		Validate.isTrue(totalExportImportVolumeDollar != null);

		Double rmbExchangeFate = getInputOrAccountingIndicatorValue(indicatorAccountVO, year,
				rmbExchangeFateIndicatorName);
		Validate.isTrue(rmbExchangeFate != null);

		return totalExportImportVolumeDollar * rmbExchangeFate / 100;

	}

	/**
	 * 获得江苏省进出口总额可比价人民币单位
	 * 
	 * @param indicatorAccountVO
	 * @return
	 */

	private Double getRmbTotalExportImportVolumeCp(IndicatorAccountVO indicatorAccountVO, int year) {
		Double totalExportImportVolume = getRmbTotalExportImportVolume(indicatorAccountVO, year);

		Double cpi = getInputOrAccountingIndicatorValue(indicatorAccountVO, indicatorAccountVO.getYear(),
				CPIIndicatorName);

		Validate.isTrue(cpi != null);

		return totalExportImportVolume * 100 / cpi;
	}

}
